package com.media.publish.video_view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


import com.media.R;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import com.media.publish.adapter.QNStickerPictureAdapter;

/**
 * 编辑模块 添加贴纸
 */
public class QNStickerBottomView extends BaseBottomView implements QNStickerPictureAdapter.OnStickerPictureClickListener {

    private View mEditorSticker;
    private RecyclerView mImageListView;
    private OnImageSelectedListener mOnImageSelectedListener;
    public static final String STICKER_FOLDER = "stickers";//贴纸文件名
    private ArrayList<String> mStickerPathList = new ArrayList<String>();// 图片路径列表

    public QNStickerBottomView(@NonNull Context context) {
        super(context);
        init();
    }

    public QNStickerBottomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public QNStickerBottomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void init() {

        mEditorSticker = LayoutInflater.from(mContext).inflate(R.layout.editor_sticker_view, this);
        mImageListView = mEditorSticker.findViewById(R.id.recycler_paint_image);
        //初始化贴纸
        getStickerRes();
        QNStickerPictureAdapter mQNStickerPictureAdapter = new QNStickerPictureAdapter(LayoutInflater.from(mContext));
        mQNStickerPictureAdapter.setOnStickerPictureClickListener((assetsPath, drawable) -> {
            if (mOnImageSelectedListener != null)
                mOnImageSelectedListener.onImageSelected(drawable,assetsPath);
        });
        mImageListView.setAdapter(mQNStickerPictureAdapter);
        mQNStickerPictureAdapter.setStickList(mStickerPathList);
    }

    @Override
    public boolean isPlayerNeedZoom() {
        return false;
    }


    public void getStickerRes() {
        try {
            String[] files = mContext.getAssets()
                    .list(STICKER_FOLDER);
            if (files != null && files.length > 0) {
                for (String name : files) {
                    mStickerPathList.add(STICKER_FOLDER + File.separator + name);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void setOnImageSelectedListener(OnImageSelectedListener listener) {
        mOnImageSelectedListener = listener;
    }

    @Override
    public void onStickerPicture(String assetsPath, Drawable drawable) {
        if (mOnImageSelectedListener != null && drawable != null) {
            mOnImageSelectedListener.onImageSelected(drawable, assetsPath);
        }
    }

    public interface OnImageSelectedListener {
        void onImageSelected(Drawable drawable, String assetsPath);
    }
}
